import java.util.HashSet;
import java.util.Set;

/**
 * @author LKQ
 * @date 2022/2/20 14:25
 * @description
 */
public class Solution {
    public static void main(String[] args) {

    }
    public int longestConsecutive(int[] nums) {
        Set<Integer> set = new HashSet<>();
        // hash存储数组
        for (int num : nums) {
            set.add(num);
        }
        // 最长序列长度
        int longest = 0;
        for (int num: set) {
            if (!set.contains(num - 1)) {
                // 序列开始元素
                int currentNum = num;
                int currentStreak = 1;
                while (set.contains(currentNum + 1)) {
                    currentNum++;
                    currentStreak += 1;
                }
                longest = Math.max(longest, currentStreak);
            }
        }
        return longest;
    }
}
